<?php

namespace App\Http\Controllers\Api\V1\Frontend\Service;

use App\Models\BpmInsUserList;
use App\Models\BpmMaindata;
use Illuminate\Http\Request;
use App\Services\ListService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class IndexController extends Controller
{
    /**
     * 获取到服务单的节点信息
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getNode(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'   => 'required|string'
        ])->validate();

        $data = $listService->getNode($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     *
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function handleNode(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'       => 'required|string',
            'user_id'         => 'nullable|string',
            'remark'          => 'nullable|string',
            'target_node_id'  => 'required|string',
            'current_node_id' => 'required|string',
        ],[
            'server_id.required'        => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'target_node_id.required'   => '请选择后续节点',
//            'user_id.required'          => '当前操作人识别有误，请重新登陆后操作',
//            'remark.required'           => '请填写 审批意见',
        ])->validate();

        $data = $listService->handleNode($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 网络硬件评估，获取确认审批人
     * @param ListService $listService
     * @return mixed
     */
    public function hardwareApprover(ListService $listService)
    {

        $data = $listService->hardwareApprover();

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 服务单列表数据展示
     * 默认展示的待办服务单
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function index(Request $request,ListService $listService)
    {
        //默认展示我的服务单
        if (!$request->filled('type')) {
            $request->offsetSet('type',1);
        }

        Validator::make($request->all(),[
            'type'   => 'nullable|int|in:1,2,3',
            'search' => 'nullable|string',
            'date'   => 'nullable|date_format:Y-m-d',
            'page'   => 'nullable|int'
        ])->validate();

        $data = $listService->getList($request->input('type'),$request->only('search','date'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 服务单基本详情信息
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function details(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'id' => 'required|string',
        ])->validate();

        $data = $listService->getReportDetails($request->input('id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 审批记录信息展示
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function approvalRecord(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'id' => 'required|string',
        ])->validate();

        $data = $listService->getApprovalRecord($request->input('id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求评估审核节点，保存自定义节点保存的审批路线
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function customApproverUpdate(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'           => 'required|string',
            'next_point'          => 'required|string',
        ],[
            'next_point.required'  => '下一审批节点 不能为空',
        ])->validate();

        $data = $listService->customApproverUpdate($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'保存成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * oa 记录展示
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function oaRecord(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'id' => 'required|string',
        ])->validate();

        $data = $listService->getOaRecord($request->input('id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解列表展示
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecompose(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'id' => 'required|string',
        ])->validate();

        $data = $listService->getTaskDecompose($request->input('id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解单详情数据展示
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeDetails(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'id' => 'required|string',
        ])->validate();

        $data = $listService->getTaskDecomposeDetails($request->input('id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解单模糊查询
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeSearch(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'search' => 'nullable|string|max:50'
        ])->validate();

        $data = $listService->searchTask($request->input('search'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解单创建
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeCreate(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'           => 'required|string',
            'involve_system_id'   => 'required|string',
            'involve_system_name' => 'required|string',
            'plate_id'            => 'required|string',
            'plate_name'          => 'required|string',
            'unit_id'             => 'required|string',
            'unit_name'           => 'required|string',
            'request_data'        => 'required|string',
            'develop_data'        => 'required|string',
            'develop_name_id'     => 'required|string',
            'develop_name'        => 'required|string',
        ],[
            'involve_system_name.required'  => '涉及系统 不能为空',
            'plate_name.required'           => '板块名称 不能为空',
            'develop_name.required'         => '系统负责人 不能为空',
            'plate_id.required'             => '平台 不能为空',
            'unit_id.required'              => '单位 不能为空',
            'request_data.required'         => '需求人员信息 不能为空'
        ])->validate();

        $data = $listService->taskDecomposeCreate($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'创建成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解单编辑
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeUpdate(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'decompose_id'        => 'required|string',
            'involve_system_id'   => 'required|string',
            'involve_system_name' => 'required|string',
            'plate_id'            => 'required|string',
            'plate_name'          => 'required|string',
            'unit_id'             => 'required|string',
            'unit_name'           => 'required|string',
            'request_data'        => 'required|string',
            'develop_data'        => 'required|string',
            'develop_name_id'     => 'required|string',
            'develop_name'        => 'required|string',
        ],[
            'involve_system_name.required'  => '涉及系统 不能为空',
            'plate_name.required'           => '板块名称 不能为空',
            'develop_name.required'         => '系统负责人 不能为空',
            'plate_id.required'             => '平台 不能为空',
            'unit_id.required'              => '单位 不能为空',
            'request_data.required'         => '需求人员信息 不能为空'
        ])->validate();

        $data = $listService->taskDecomposeUpdate($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'编辑成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解单删除
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeDestroy(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'task_id'    => 'required|string',
            'server_id'  => 'required|string'
        ])->validate();

        $data = $listService->taskDecomposeDestroy($request->only('task_id','server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'删除成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 设置需求分解单 牵头单位列表
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeLeaderList(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'  => 'required|string'
        ])->validate();

        $data = $listService->getTaskDecomposeLeaderList($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }



    /**
     * 获取可选的牵头团队列表
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getLeadTeamList(Request $request,ListService $listService)
    {
        $data = $listService->getLeadTeamList();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 设置需求分解单 牵头单位
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException|\DOMException
     */
    public function taskDecomposeLeader(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'task_id'   => 'required|string',
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->getTaskDecomposeLeader($request->only('server_id','task_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'设置成功');
        }

        return $this->failed($data['message']);
    }



    /**
     * 更新需求分解单 牵头团队
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException|\DOMException
     */
    public function updateTaskDecomposeLeader(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'     => 'required|string',
            'leadTeamName'  => 'required|string',
            'leadTeamID'    => 'required|integer',
            'leaderName'    => 'required|string',
            'leadUserName'  => 'required|string',
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'leadTeamName.required' => '请选择 牵头团队',
            'leadTeamID.required'   => '牵头团队 识别有误，请重新操作',
            'leaderName.required'   => '团队长 识别有误，请重新选择牵头团队',
            'leadUserName.integer'  => '团队长 识别有误，请重新选择牵头团队',
            'leadUserName.required' => '团队长 识别有误，请重新选择牵头团队',
        ])->validate();

        $data = $listService->updateTaskDecomposeLeader($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'更新成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到审核架构
     * @param ListService $listService
     * @return mixed
     */
    public function taskDecomposeLeaderFramework(ListService $listService)
    {
        $data = $listService->getTaskDecomposeLeaderFramework();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解设置 牵头单位 设置 审核架构
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeLeaderUpdateFramework(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'framework'  => 'nullable|array',
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->taskDecomposeLeaderUpdateFramework($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'设置成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 工作量评估
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function workAssess(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string',
        ])->validate();

        $data = $listService->getWorkAssess($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到工作量评估详情
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function workAssessDetails(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'assess_id' => 'required|string',
        ])->validate();

        $data = $listService->getWorkAssessDetails($request->input('assess_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到预计人月详情展示
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function estimateWorking(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'            => 'required|string',
            'implement_content_id' => 'required|string',
            'system_id'            => 'required|string',
            'function_name'        => 'required|string',
        ],[
            'server_id.required'            => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'implement_content_id.required' => '实施内容识别有误，请先选择实施内容后操作',
            'system_id.required'            => '系统 不能为空',
            'function_name.required'        => '名称 不能为空'
        ])->validate();

        $data = $listService->getEstimateWorking($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求人月的创建
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function createEstimateWorking(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'            => 'required|string',
            'assess_id'            => 'required|string',
            'implement_content_id' => 'required|string',
            'system_id'            => 'required|string',
            'function_name'        => 'required|string',
            'describe'             => 'required|string|max:100',
            'analyze'              => 'nullable|numeric',
            'config'               => 'nullable|numeric',
            'design'               => 'nullable|numeric',
            'developer'            => 'nullable|numeric',
            'test'                 => 'nullable|numeric',
            'project'              => 'nullable|numeric',
            'price'                => 'nullable|numeric',
            'estimate_id'          => 'filled|string',
        ],[
            'server_id.required'            => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'assess_id.required'            => '需求识别有误，请重新操作',
            'implement_content_id.required' => '实施内容识别有误，请先选择实施内容后操作',
            'system_id.required'            => '系统 不能为空',
            'function_name.required'        => '名称 不能为空',
            'describe.required'             => '描述 不能为空',
//            'analyze.required'          => '需求分析 不能为空',
//            'config.required'           => '系统配置 不能为空',
//            'design.required'           => '系统设计 不能为空',
//            'developer.required'        => '系统开发 不能为空',
//            'test.required'             => '系统测试 不能为空',
//            'project.required'          => '项目管理 不能为空',
//            'price.required'            => '单价 不能为空',
        ])->validate();
        if (!$request->filled('analyze')) {
            $request->offsetSet('analyze',"0.00");
        }
        if (!$request->filled('config')) {
            $request->offsetSet('config',"0.00");
        }
        if (!$request->filled('design')) {
            $request->offsetSet('design',"0.00");
        }
        if (!$request->filled('developer')) {
            $request->offsetSet('developer',"0.00");
        }
        if (!$request->filled('test')) {
            $request->offsetSet('test',"0.00");
        }
        if (!$request->filled('project')) {
            $request->offsetSet('project',"0.00");
        }

        $data = $listService->createEstimateWorking($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'保存成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 工作量评估实施内容条件查询
     * @param ListService $listService
     * @return mixed
     */
    public function workAssessConditionResourcesImplementation(ListService $listService)
    {
        $data = $listService->getWorkAssessConditionResourcesImplementation();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 工作量评估资源类型条件查询
     * @param ListService $listService
     * @return mixed
     */
    public function workAssessConditionResources(ListService $listService)
    {
        $data = $listService->getWorkAssessConditionResourcesResources();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /** 工作量评估厂商条件查询
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function workAssessConditionManufacturer(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'type_id'   => 'nullable|int',
            'system_id' => 'required|string'
        ])->validate();

        $data = $listService->getWorkAssessConditionManufacturer($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 编辑工作量评估
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function updateWorkAssess(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'assess_id'           => 'required|string',
            'implement_content'   => 'required|string',
            'implement_content_id'=> 'required|int',
            'function_name'       => 'required|string',
            'source_type'         => 'required|string',
            'source_type_id'      => 'required|int',
            'cycle'               => 'required|string',
            'start_date'          => 'required|date_format:Y-m-d',
            'check_date'          => 'required|date_format:Y-m-d',
            'manufacturer'        => 'nullable|string',
            'manufacturer_id'     => 'nullable|int',
            'price'               => 'nullable|numeric',
            'workflow'            => 'nullable|numeric',
            'estimate_it_amount'  => 'nullable|string',
            'is_net'              => 'nullable|boolean',
            'is_netted'           => 'nullable|boolean',
            'is_other'            => 'nullable|boolean',
            'is_big_file'         => 'nullable|boolean',
            'data_save_request'   => 'nullable|string',
            'reliable_request'    => 'nullable|string',
            'project_number'      => 'nullable|string'

        ],[
            'implement_content.required'   => '实施内容不能为空',
            'function_name.required'       => '功能名称不能为空',
            'source_type.required'         => '资源类型不能为空',
            'start_date.required'          => '预计启动时间不能为空',
            'cycle.required'               => '实施周期(月)不能为空',
            'check_date.required'          => '预计提交用户验收时间不能为空'
        ])->validate();

        $data = $listService->updateWorkAssess($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'编辑成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 工作量评估创建
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function createWorkAssess(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'assess_id'            => 'required|string',
            'implement_content'    => 'required|string',
            'implement_content_id' => 'required|int',
            'function_name'        => 'required|string',
            'source_type'          => 'required|string',
            'source_type_id'       => 'required|int',
            'cycle'                => 'required|string',
            'start_date'           => 'required|date_format:Y-m-d',
            'check_date'           => 'required|date_format:Y-m-d',
            'manufacturer'         => 'nullable|string',
            'manufacturer_id'      => 'nullable|int',
            'price'                => 'nullable|numeric',
            'workflow'             => 'nullable|numeric',
            'estimate_it_amount'   => 'nullable|string',
            'is_net'               => 'nullable|boolean',
            'is_netted'            => 'nullable|boolean',
            'is_other'             => 'nullable|boolean',
            'is_big_file'          => 'nullable|boolean',
            'data_save_request'    => 'nullable|string',
            'reliable_request'     => 'nullable|string',
            'project_number'       => 'nullable|string'
        ],[
            'implement_content.required'   => '实施内容不能为空',
            'function_name.required'       => '功能名称不能为空',
            'source_type.required'         => '资源类型不能为空',
            'start_date.required'          => '预计启动时间不能为空',
            'cycle.required'               => '实施周期(月)不能为空',
            'check_date.required'          => '预计提交用户验收时间不能为空'
        ])->validate();

        $data = $listService->createWorkAssess($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'创建成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 删除工作量评估
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function destroyWorkAssess(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'assess_id' => 'required|string',
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->destroyWorkAssess($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到it 服务单 网络硬件评估信息
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getHardware(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->getHardware($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);

    }

    public function createHardware(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'                    => 'required|string',
            'database_server_id'           => 'required|integer',
            'database_server_name'         => 'required|string',
            'cpu_quantity'                 => 'required|integer|min:0',
            'memory_quantity'              => 'required|integer|min:0',
            'database_expect_invest'       => 'required|numeric|min:0',
            'application_server'           => 'required|integer|min:0',
            'application_server_invest'    => 'required|numeric|min:0',
            'san_storage_space'            => 'required|integer|min:0',
            'san_storage_space_invest'     => 'required|numeric|min:0',
            'nac_storage_space'            => 'required|integer|min:0',
            'nac_storage_space_invest'     => 'required|numeric|min:0',
            'dmz_server'                   => 'required|integer|min:0',
            'dmz_server_invest'            => 'required|numeric|min:0',
            'dedicated_line_expect_invest' => 'required|numeric|min:0',
        ],[
            'server_id.required'                    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'database_server_id.required'           => '请选择 数据库服务器',
            'cpu_quantity.required'                 => 'CUP数量 不能为空',
            'memory_quantity.required'              => '内存数量 不能为空',
            'database_expect_invest.required'       => '数据库服务器预计投入 不能为空',
            'application_server.required'           => '应用服务器数量 不能为空',
            'application_server_invest.required'    => '应用服务器预计投入 不能为空',
            'san_storage_space.required'            => 'SAN存储空间 不能为空',
            'san_storage_space_invest.required'     => 'SAN存储空间预计投入 不能为空',
            'nac_storage_space.required'            => 'NAC存储空间 不能为空',
            'nac_storage_space_invest.required'     => 'NAC存储空间预计投入 不能为空',
            'dmz_server.required'                   => 'DMZ服务器数量 不能为空',
            'dmz_server_invest.required'            => 'DMZ服务器预计投入 不能为空',
            'dedicated_line_expect_invest.required' => '专线预计投入 不能为空',
            'cpu_quantity.min'                      => 'CUP数量 应填非负整数',
            'cpu_quantity.integer'                  => 'CUP数量 应填非负整数',
            'memory_quantity.min'                   => '内存数量 应填非负整数',
            'memory_quantity.integer'               => '内存数量 应填非负整数',
            'database_expect_invest.min'            => '数据库服务器预计投入 应为非负数',
            'application_server.min'                => '应用服务器数量 应填非负整数',
            'application_server.integer'            => '应用服务器数量 应填非负整数',
            'application_server_invest.min'         => '应用服务器预计投入 应为非负数',
            'san_storage_space.min'                 => 'SAN存储空间 应填非负整数',
            'san_storage_space.integer'             => 'SAN存储空间 应填非负整数',
            'san_storage_space_invest.min'          => 'SAN存储空间预计投入 应为非负数',
            'nac_storage_space.min'                 => 'NAC存储空间 应填非负整数',
            'nac_storage_space.integer'             => 'NAC存储空间 应填非负整数',
            'nac_storage_space_invest.min'          => 'NAC存储空间预计投入 应为非负数',
            'dmz_server.min'                        => 'DMZ服务器数量 应填非负整数',
            'dmz_server.integer'                    => 'DMZ服务器数量 应填非负整数',
            'dmz_server_invest.min'                 => 'DMZ服务器预计投入 应为非负数',
            'dedicated_line_expect_invest.min'      => '专线预计投入 应填非负整数',
            'dedicated_line_expect_invest.integer'  => '专线预计投入 应填非负整数'
        ])->validate();

        $data = $listService->createHardware($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 网络处硬件评估数据库服务器列表
     * @param ListService $listService
     * @return mixed
     */
    public function getHardwareServer(ListService $listService)
    {
        $data = $listService->getHardwareServer();

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求评估汇总列表 获取
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getRequirement(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->getRequirement($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求评估汇总 编辑
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function editRequirement(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->getRequirement($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求评估汇总详情信息
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function requirementDetails(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->getRequirementDetails($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求评估汇总 获取项目负责人
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function requirementHeadUser(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'     => 'required|string',
            'lead_team_id'  => 'required|integer'
        ],[
            'server_id.required'      => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'lead_team_id.required'   => '请选择 开发牵头团队识别错误',
            'lead_team_id.integer'    => '请选择 开发牵头团队识别错误'
        ])->validate();

        $data = $listService->getRequirementHeadUser($request->input('server_id'),$request->input('lead_team_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求评估汇总 获取到牵头单位
     * @param ListService $listService
     * @return mixed
     */
    public function requirementHeadTeam(ListService $listService)
    {
        $data = $listService->getRequirementHeadTeam();

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取选择牵头团队之后的涉及团队、系统负责人人 两个字符串
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getRelatedTeamAndSystemLeaderStr(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'     => 'required|string',
            'lead_team_id'  => 'required|integer'
        ],[
            'server_id.required'      => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'lead_team_id.required'   => '请选择 开发牵头团队',
            'lead_team_id.integer'    => '开发牵头团队识别错误'
        ])->validate();
        $serverId = $request->get("server_id");
        $leadTeamID = $request->get("lead_team_id");

        $data = $listService->getRelatedTeamAndSystemLeaderStr($serverId, $leadTeamID);

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }
    /**
     * 需求评估汇总 修改牵头单位和项目负责人
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function requirementDetailsEdit(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'                     => 'required|string',
            'project_leader'                => 'required|string',
            'project_leader_id'             => 'required|integer',
            'development_leader_team'       => 'required|string',
            'development_leader_team_id'    => 'required|integer',
            'changed'                       => 'nullable|integer',  // 牵头团队与项目负责人是否有更新，1表示有更新，其他标识没有更新
            'system_name_id'                => 'nullable|string', // 涉及系统ID拼接字符串
            'system_name'                   => 'nullable|string', // 涉及系统名称拼接字符串
            'leader_id'                     => 'nullable|string', // 系统负责人ID拼接字符串
            'leader'                        => 'nullable|string', // 系统负责人名称拼接字符串
            'text76'                        => 'nullable|numeric', // 预计IT投入
            'projectAmount'                 => 'nullable|numeric'  // 立项金额
        ],[
            'server_id.required'                    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'project_leader.required'               => '请选择 项目负责人',
            'project_leader_id.required'            => '项目负责人识别错误',
            'project_leader_id.integer'             => '项目负责人识别错误',
            'development_leader_team.required'      => '请选择 开发牵头团队',
            'development_leader_team_id.required'   => '开发牵头团队识别错误',
            'development_leader_team_id.integer'    => '开发牵头团队识别错误'
        ])->validate();

        $data = $listService->requirementDetailsEdit($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 需求分析列表
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getRequirementAnalysis(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();
        $type   = $request->filled('show_all') ?$request->input('show_all') : 0;  // 是否显示全部，1显示全部，0显示与当前登陆人相关的需求分析数据
        $nodeId = $request->filled('node_id')  ?$request->input('node_id')  : '';
        $data   = $listService->getRequirementAnalysis($request->input('server_id'), $type, $nodeId);

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 初始化需求分析数据
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function initRequirementAnalysis(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
        ])->validate();

        $data = $listService->initRequirementAnalysis($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 需求分析填报
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function editRequirementAnalysis(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_analysis_id'     => 'required|string',
            'is_confirm'             => 'required|boolean',
            'demand_specification'   => 'required|string',
            'advice_confirm_time'    => 'required|date_format:Y-m-d',
            'expect_acceptance_time' => 'required|date_format:Y-m-d'
        ],[
            'server_analysis_id.required'       => '需求分析识别有误，请重新操作',
            'is_confirm.required'               => '请选择 是否需要确认',
            'is_confirm.boolean'                => '是否需要确认 值解析错误',
            'demand_specification.required'     => '需求规则说明书 不能为空',
            'advice_confirm_time.required'      => '请选择 建议用户确认反馈时间',
            'expect_acceptance_time.required'   => '请选择 建议提交用户验收时间'
        ])->validate();

        $data = $listService->editRequirementAnalysis($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 初始化用户验收数据
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function initSystemHarvest(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
        ])->validate();

        $data = $listService->initSystemHarvest($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 用户验收列表
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getSystemHarvest(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $type = $request->filled('show_all') ?$request->input('show_all') :0;  // 是否显示全部，1显示全部，0显示与当前登陆人相关的用户验收数据
        $nodeId = $request->filled('node_id')  ?$request->input('node_id')  : '';
        $data = $listService->getSystemHarvest($request->input('server_id'), $type, $nodeId);

        $personDate = $listService->getPersonDate($request->all());
        if($personDate['code'] == 200){
            $data['person_date'] = $personDate['data']['person_date'];
        }
        if ($data['code'] == 200){
            $res['list'] = $data['data'];
            $res['person_date'] = $data['person_date'];
            return $this->successful($res,$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 用户验收填报
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function editSystemHarvest(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_harvest_id' => 'required|string',
            'address'           => 'nullable|string',
            'username'          => 'nullable|string',
            'password'          => 'nullable|string',
            'instruction'       => 'nullable|string'
        ],[
            'server_harvest_id.required'    => '涉及系统 不能为空，请检查前置步骤或重新进入IT服务单',
            'username.required'             => '请填写 用户名',
            'address.string'                => '验证地址 格式错误',
            'username.string'               => '请填写 格式错误',
            'password.string'               => '请填写 格式错误',
            'instruction.string'            => '请填写 格式错误'
        ])->validate();

        $data = $listService->editSystemHarvest($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 更新建议用户确认反馈时间
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function editPersonDate(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'     => 'required|string',
            'person_date'   => 'required|date_format:Y-m-d',
        ],[
            'server_id.required'    => 'IT服务单识别失败，请重新进入IT服务单后操作',
            'person_date.required'  => '请填写 建议用户验收反馈时间',
        ])->validate();

        $data = $listService->editPersonDate($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 初始化上线信息数据
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function initSystemOnline(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
        ])->validate();

        $data = $listService->initSystemOnline($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 上线列表
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function getSystemOnline(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $type = $request->filled('show_all') ?$request->input('show_all') :0;  // 是否显示全部，1显示全部，0显示与当前登陆人相关的用户验收数据
        $nodeId = $request->filled('node_id')  ?$request->input('node_id')  : '';
        $data = $listService->getSystemOnline($request->input('server_id'), $type, $nodeId);

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /** it 服务单 上线填报
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function editSystemOnline(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_online_id' => 'required|string',
            'is_online'        => 'required|boolean',
            'online_date'      => 'nullable|date_format:Y-m-d'
        ],[
            'server_online_id.required' => '涉及系统 不能为空，请检查前置步骤或重新进入IT服务单',
            'is_online.required'        => '请选择 是否上线',
            'is_online.boolean'         => '是否上线 识别错误',
//            'online_date.required'      => '请选择 上线时间'
        ])->validate();

        $data = $listService->editSystemOnline($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 需求分解 获取项目负责人
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function taskDecomposeProjectLeader(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string'
        ])->validate();

        $data = $listService->getTaskDecomposeProjectLeader($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 需求分解 设置项目负责人
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function editTaskDecomposeProjectLeader(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'      => 'required|string',
            'project_leader' => 'required|string',
            'user_id'        => 'required|string',
        ],[
            'server_id.required'        => '服务单识别失败，请检查前置步骤或重新进入IT服务单',
            'project_leader.required'   => '请选择 项目负责人',
        ])->validate();

        $data = $listService->editTaskDecomposeProjectLeader($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 架构审核 审核意见
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function architectureApproval(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id'      => 'required|string',
            'opinion'        => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'opinion.required'    => '请填写 审核意见',
        ])->validate();

        $data = $listService->architectureApproval($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    public function workAssessUsers(Request $request,ListService $listService)
    {
        $data = $listService->getWorkAssessUsers($request->input('server_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 根据节点id 获取到页面控制的权限展示
     * @param Request $request
     * @return mixed
     * @throws ValidationException
     */
    public function getNodeShowModule(Request $request): array
    {
        Validator::make($request->all(),[
            'server_id'      => 'nullable|string',
        ])->validate();
        $nodeString = $request->input('node_id') ?? '';

        if (empty($nodeString)) {

            $nodeString = BpmMaindata::where('WF_OrUnid',$request->input('server_id',''))
                ->value('WF_CurrentNodeid');
            if (empty($nodeString)) {
                return [
                    'requirement_decompose',
                    'workload_assessment',
                    'network_hardware',
                    'requirement_statistics',
                    'requirement_analysis',
                    'user_check',
                    'system_online'
                ];
            }
        }

        $nodeIdArray = explode(",",$nodeString);

        $nodeIdArray = array_filter($nodeIdArray,function ($item) {
            return substr($item,0,1) == 'T';
        });

        $result = [];

        $defaultCount = 0;

        foreach ($nodeIdArray as $nodeId) {

            if (in_array($nodeId,['T10305'])) {
                $modules = [
                ];
            } else if (in_array($nodeId,['T10013','T10340','T10077'])) {
                $modules = [
                    'requirement_decompose'
                ];
            } else if (in_array($nodeId,['T10015','T10067'])) {
                $modules = [
                    'requirement_decompose',
                    'workload_assessment'
                ];
            } else if (in_array($nodeId,['T10016','T10335'])) {
                $modules = [
                    'requirement_decompose',
                    'workload_assessment',
                    'network_hardware'
                ];
            } else if (in_array($nodeId,['T10017','T10082','T10036','T10082','T10018','T10086'])) {
                $modules = [
                    'requirement_decompose',
                    'workload_assessment',
                    'network_hardware',
                    'requirement_statistics'
                ];
            } else if (in_array($nodeId,['T10092','T10091','T10347','T10088','T10269'])){
                $modules = [
                    'requirement_decompose',
                    'workload_assessment',
                    'network_hardware',
                    'requirement_statistics',
                    'requirement_analysis'
                ];
            } else if (in_array($nodeId,['T10120','T10351','T10093','T10099','T10272','T10272'])) {
                $modules = [
                    'requirement_decompose',
                    'workload_assessment',
                    'network_hardware',
                    'requirement_statistics',
                    'requirement_analysis',
                    'user_check'
                ];
            } else {
                $modules = [
                    'requirement_decompose',
                    'workload_assessment',
                    'network_hardware',
                    'requirement_statistics',
                    'requirement_analysis',
                    'user_check',
                    'system_online'
                ];
            }

            if (count($modules) > $defaultCount) {
                $defaultCount = count($modules);
            }

            $result[count($modules)] = $modules;
        }

        return $result[$defaultCount];
    }


    /**
     * it 服务单 返回需求分析前置处理
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function backReqConfirm(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string',
            'doc_id'    => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'doc_id.required'       => '请选择至少一个需求分析数据',
        ])->validate();

        $data = $listService->backReqConfirm($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 返回需求实施前置处理
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function backUserAcceptance(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string',
            'doc_id'    => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'doc_id.required'       => '请选择至少一个用户验收数据',
        ])->validate();

        $data = $listService->backUserAcceptance($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * it 服务单 返回上线信息前置处理
     * @param Request $request
     * @param ListService $listService
     * @return mixed
     * @throws ValidationException
     */
    public function backOnlineInfo(Request $request,ListService $listService)
    {
        Validator::make($request->all(),[
            'server_id' => 'required|string',
            'doc_id'    => 'required|string'
        ],[
            'server_id.required'    => 'IT服务单识别有误，请重新进入IT服务单后操作',
            'doc_id.required'       => '请选择至少一个上线信息数据',
        ])->validate();

        $data = $listService->backOnlineInfo($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }
}
